package cn.zyx.oa.entity;

import cn.zyx.common.entity.DataEntity;
import cn.zyx.common.utils.CollectionsUtils;
import cn.zyx.common.utils.sys.IdGenUtils;
import cn.zyx.sys.entity.User;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import java.util.List;

/**
 * 通知通告Entity
 *
 * @author zyx
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class OaNotify extends DataEntity<OaNotify> {

    private static final long serialVersionUID = 1L;

    @Length(min = 0, max = 1, message = "类型长度必须介于 0 和 1 之间")
    private String type;        // 类型
    @Length(min = 0, max = 200, message = "标题长度必须介于 0 和 200 之间")
    private String title;        // 标题
    private String content;        // 类型
    @Length(min = 0, max = 2000, message = "附件长度必须介于 0 和 2000 之间")
    private String files;        // 附件
    @Length(min = 0, max = 1, message = "状态长度必须介于 0 和 1 之间")
    private String status;        // 状态
    private String readNum;        // 已读
    private String unReadNum;    // 未读

    private boolean isSelf;        // 是否只查询自己的通知

    private String readFlag;    // 本人阅读状态

    private List<OaNotifyRecord> oaNotifyRecordList = Lists.newArrayList();

    public OaNotify() {
        super();
    }

    public OaNotify(String id) {
        super(id);
    }

    /**
     * 获取通知发送记录用户ID
     *
     * @return
     */
    public String getOaNotifyRecordIds() {
        return CollectionsUtils.extractToString(oaNotifyRecordList, "user.id", ",");
    }

    /**
     * 设置通知发送记录用户ID
     *
     * @return
     */
    public void setOaNotifyRecordIds(String oaNotifyRecord) {
        this.oaNotifyRecordList = Lists.newArrayList();
        for (String id : StringUtils.split(oaNotifyRecord, ",")) {
            OaNotifyRecord entity = new OaNotifyRecord();
            entity.setId(IdGenUtils.uuid());
            entity.setOaNotify(this);
            entity.setUser(new User(id));
            entity.setReadFlag("0");
            this.oaNotifyRecordList.add(entity);
        }
    }

    /**
     * 获取通知发送记录用户Name
     *
     * @return
     */
    public String getOaNotifyRecordNames() {
        return CollectionsUtils.extractToString(oaNotifyRecordList, "user.name", ",");
    }

    /**
     * 设置通知发送记录用户Name
     *
     * @return
     */
    public void setOaNotifyRecordNames(String oaNotifyRecord) {
        // 什么也不做
    }
}